<?php

class ctrl_pdd_pddLogistics extends L {

    /**
     * 描述 ：物流公司列表页面展示
     * 作者 ：Zoro.Zhu
    **/
    public function index(){
        $this->view->_pagingHtml = $this->paging();
        $this->view->headTitle = '物流公司列表';
        $this->display('/html/pdd/pddLogistics.html');
    }

    /**
     * 描述 : 获取分页
     * 作者 : Zoro.Zhu
     */
    public function &paging($params = array()){
        $sql = model_pddLogistics::searchSql($params);
        $config = array( 
            '快递公司编码' => array(
                '_attr' => array(
                    'body' => '{`id`}',
                    'sort' => 'id'
                )
            ),
            '物流公司代码' => array(
                '_attr' => array(
                    'body' => '{`code`}',
                    'sort' => 'code'
                )
            ),
            '快递公司名称' => array(
                '_attr' => array(
                    'body' => '{`logisticsCompany`}',
                    'sort' => 'logisticsCompany'
                )
            ),
            '是否有效，0无效，1有效' => array(
                '_attr' => array(
                    'body' => '{`available`}',
                    'sort' => 'available'
                )
            ),
            '操作'=> "<button class=\"btn btn-oper btn-danger\" type=\"button\" onclick=\"pddLogisticsObj.pddLogisticsUpInfo('{`id`}');\">
                <span class=\"fa fa-plus\"></span>修改
            </button>",
            '_attr' => array(
                "attr" => array (
                    "table" => 'id=pddLogisticsList class="table table-hover table-condensed table-striped table-scroll jsHoverDetails textC"',
                    "btr" => "align='center' class='of-paging_item_odd tr_{`id`}'",
                ),
                'data' => &$sql,
                'params' => &$params,
                'call' => array($this, 'asCall'),
                'method' => __METHOD__,
                'save' => 'auto',
                'empty' => '查无此数据'
            )
        );
        return $this->_com->paging($config);
    }

    /**
     * 描述 : 整理数据
     * 作者 : Zoro.Zhu
     */
    public function asCall(&$data){
        foreach($data['data'] as &$val){

        }
    }

    /**
     * 描述 : 新增和修改的页面展示
     * 作者 : Zoro.Zhu
     */
    public function pddLogisticsUpInfo(){
        if(!empty($_REQUEST['id'])) {
            $pddLogistics = model_pddLogistics::getKeyInfo($_REQUEST['id']);
            if(!empty($pddLogistics)){ 
                $this->view->id = $pddLogistics[0]['id'];
                $this->view->code = $pddLogistics[0]['code'];
                $this->view->logisticsCompany = $pddLogistics[0]['logisticsCompany'];
                $this->view->available = $pddLogistics[0]['available'];
            }
        }
        $this->display('/html/pdd/pddLogisticsUpInfo.html');
    }

    /**
     * 描述 ：新增或修改数据
     * 作者 ：Zoro.Zhu
    **/
    public function pddLogisticsUpd(){
        //主键为空是新增，否则就是修改
        if(empty($_POST['id'])){
            $result = model_pddLogistics::insertSql($_POST);
        }else{
            $result = model_pddLogistics::updateSql($_POST);
        }
        if($result > 0){
            $key = 'pddLogisticNamesList';
            of_base_com_kv::del($key);
            $key = 'pddLogisticsList';
            of_base_com_kv::del($key);
            return array('state' => 200, 'info' => '操作成功');
        }else{
            return array('state' => 301, 'info' => '操作无效');
        }
    }

    /**
     * 描述 ：按条件导出数据
     * 作者 ：Zoro.Zhu
    **/
    public function exportCsv(){
        $sql = model_pddLogistics::searchSql(array('search' => $_GET));
        $data = L::sql($sql);
        of_base_com_csv::download('导出数据' . time());                     //导出数据的文件名
        of_base_com_csv::download(array('快递公司编码','物流公司代码','快递公司名称','是否有效',));
        foreach ($data as $row){
            of_base_com_csv::download(array($row['id'],$row['code'],$row['logisticsCompany'],$row['available'],));
        }
    }

    /**
     * 描述 ：刷新物流列表
     * 作者 ：Zoro.Zhu
    **/
    public function getList(){
        $token = model_vsAccount::getAccountToken(['type' => 'pdd']);
        $data['accessToken'] = $token['accessToken'];
        $data['clientId'] = $token['appKey'];
        $data['clientSecret'] = $token['appSecret'];
        $list = model_api_pdd::getLogisticsCompanies($data);
        if(!empty($list)){
            $insert = "INSERT INTO `pdd_logistics`(`id`, `code`, `logisticsCompany`, `available`) VALUES ";
            foreach($list['logistics_companies'] as $row){
                $insert .= " ('{$row['id']}', '{$row['code']}', '{$row['logistics_company']}', '{$row['available']}'),";
            }
            L::sql(trim($insert, ',') . 'ON DUPLICATE KEY UPDATE 
              `code` = VALUES(`code`),
              `logisticsCompany` = VALUES(`logisticsCompany`),
              `available` = VALUES(`available`)');
            return array('state' => 200, 'info' => '刷新成功');
        }else{
            return array('state' => 300, 'info' => '刷新失败，请重试');
        }
    }

}
return true;